package aooO.crm.Service.impl;

import aooO.crm.Service.CustomerService;
import aooO.crm.Service.TranService;
import aooO.crm.common.Utils.DateUtils;
import aooO.crm.common.Utils.UUIDUtils;
import aooO.crm.mapper.CustomerMapper;
import aooO.crm.mapper.TranHistoryMapper;
import aooO.crm.mapper.TranMapper;
import aooO.crm.mapper.TranRemarkMapper;
import aooO.crm.pojo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TranServiceImpl implements TranService{

    @Autowired
    TranMapper tranMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    TranHistoryMapper tranHistoryMapper;

    @Autowired
    TranRemarkMapper tranRemarkMapper;

    @Override
    public int insertTran(Tran record) {
        String customerId = customerMapper.selectCustomerByName(record.getCustomerId());
        // 如果存在该用户，则将tran中的用户名改为对应的用户id
        if (customerId != null) {
            record.setCustomerId(customerId);
        } else {
            // 不存在该用户，则新创建用户，并将tran中的用户名改为新创建的用户id
            Customer customer = new Customer();
            customer.setOwner(record.getCreateBy());
            customer.setName(record.getCustomerId());
            customer.setId(UUIDUtils.uuid());
            customer.setCreateTime(DateUtils.FormatDateTime(new Date()));
            customer.setCreateBy(record.getCreateBy());
            customerMapper.insertCustomer(customer); // 新增用户
            record.setCustomerId(customer.getId()); // 修改联系人的用户为该用户id
        }
        // 新增交易
        tranMapper.insertTran(record);
        // 新增交易历史记录
        TranHistory tranHistory = new TranHistory();
        tranHistory.setCreateBy(record.getCreateBy());
        tranHistory.setCreateTime(DateUtils.FormatDateTime(new Date()));
        tranHistory.setTranId(record.getId());
        tranHistory.setExpectedDate(record.getExpectedDate());
        tranHistory.setMoney(record.getMoney());
        tranHistory.setStage(record.getStage());
        tranHistory.setId(UUIDUtils.uuid());
        // 新增历史记录
        int i = tranHistoryMapper.insertTransactionHistory(tranHistory);
        return i;
    }

    @Override
    public List<Tran> selectTransactionByConditionForPage(Map<String, Object> map) {
        return tranMapper.selectTransactionByConditionForPage(map);
    }

    @Override
    public int selectCountOfTransactionByCondition(Map<String, Object> map) {
        return tranMapper.selectCountOfTransactionByCondition(map);
    }

    @Override
    public Tran selectTranForDetailById(String id) {
        return tranMapper.selectTranForDetailById(id);
    }

    @Override
    public Tran selectTransactionById(String id) {
        return tranMapper.selectTransactionById(id);
    }

    @Override
    public int deleteTranByIds(String[] ids) {
        // 删除备注
        tranRemarkMapper.deleteTranRemarkByTranIds(ids);
        // 删除历史记录
        tranHistoryMapper.deleteTranHistoryByTranIds(ids);
        return tranMapper.deleteTranByIds(ids);
    }

    @Override
    public int updateTran(Tran tran) {
        String customerId = customerMapper.selectCustomerByName(tran.getCustomerId());
        // 如果存在该用户，则将tran中的用户名改为对应的用户id
        if (customerId != null) {
            tran.setCustomerId(customerId);
        } else {
            // 不存在该用户，则新创建用户，并将tran中的用户名改为新创建的用户id
            Customer customer = new Customer();
            customer.setOwner(tran.getEditBy());
            customer.setName(tran.getCustomerId());
            customer.setId(UUIDUtils.uuid());
            customer.setCreateTime(DateUtils.FormatDateTime(new Date()));
            customer.setCreateBy(tran.getEditBy());
            customerMapper.insertCustomer(customer); // 新增用户
            tran.setCustomerId(customer.getId()); // 修改联系人的用户为该用户id
        }
        // 更新交易
        tranMapper.updateTran(tran);
        // 新增更新线索历史记录
        TranHistory tranHistory = new TranHistory();
        tranHistory.setCreateBy(tran.getEditBy());
        tranHistory.setCreateTime(tran.getEditTime());
        tranHistory.setTranId(tran.getId());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setStage(tran.getStage());
        tranHistory.setId(UUIDUtils.uuid());
        // 新增历史记录
        int i = tranHistoryMapper.insertTransactionHistory(tranHistory);
        return i;
    }

    @Override
    public List<FunnelVO> selectCountOfTranGroupByStage() {
        return tranMapper.selectCountOfTranGroupByStage();
    }
}
